﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TravelLink.DAL;
using DevExpress.Web.ASPxGridView;

namespace TravelLink.WebApp.UserControl.Member.JobEditable {
    public class JobEditableQualification : JobEditableBase {

        public JobEditableQualification(bool isEn) : base(isEn) { }


        public override string AspGridViewName {
            get { return "aspxGridQualification"; }
        }

        protected override IEnumerable<object> GetSource(ResumeParam p) {
            if (Has(AspGridViewName)) {
                var memoryList = Get<TL_User_Qualification>(u => u.Status != EditableItemStatus.Delete);
                if (memoryList != null) {
                    return memoryList.Select(u => u.Item).ToList();
                }
                return null;
            }
            else {
                var db = DataContextFactory.Create();
                List<EditableItem<TL_User_Qualification>> innerList = db.TL_User_Qualification
                    .Where(u => u.ResumeId == p.ResumeIdx)
                    .Select(u => Add<TL_User_Qualification>(u, EditableItemStatus.Default))
                    .ToList();
                Set(AspGridViewName, innerList);
                return innerList.Select(u => u.Item).ToList();
            }
        }

        public override void Save(ResumeParam p) {
            var db = DataContextFactory.Create();

            var memoryList = Get<TL_User_Qualification>(u => true);
            var insetedList = memoryList.Where(u => u.Status == EditableItemStatus.Insert);
            foreach (var item in insetedList) {
                db.TL_User_Qualification.InsertOnSubmit(new TL_User_Qualification {
                    Desciption = item.Item.Desciption,
                    RowTime = item.Item.RowTime,
                    Year = item.Item.Year,
                    ResumeId = p.ResumeIdx
                });
            }

            var updateList = memoryList.Where(u => u.Status == EditableItemStatus.Edit);
            foreach (var item in updateList) {
                var skillEntity = db.TL_User_Qualification.FirstOrDefault(u => u.Idx == item.Item.Idx);
                if (skillEntity != null) {
                    skillEntity.Desciption = item.Item.Desciption;
                    skillEntity.Year = item.Item.Year;
                    skillEntity.RowTime = item.Item.RowTime;
                }
            }

            var deleteList = memoryList.Where(u => u.Status == EditableItemStatus.Delete);
            foreach (var item in deleteList) {
                db.TL_User_Qualification.DeleteAllOnSubmit(db.TL_User_Qualification.Where(u => u.Idx == item.Item.Idx));
            }

            db.SubmitChanges();

            Clear(AspGridViewName);
        }

        protected override void PreDeleting(ResumeParam p, DevExpress.Web.Data.ASPxDataDeletingEventArgs e, ASPxGridView sender) {
            var memoryList = Get<TL_User_Qualification>(u => true);

            if (memoryList == null) return;

            int key = ((int)e.Keys[0]);

            var entity = memoryList.Where(u => u.Item.Idx == key).FirstOrDefault();
            if (entity.Status != EditableItemStatus.Insert)
                entity.Status = EditableItemStatus.Delete;
            else {
                memoryList.Remove(entity);
            }

            Set(AspGridViewName, memoryList);
        }

        protected override void PreUpdating(ResumeParam p, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e, ASPxGridView sender) {
            var memoryList = Get<TL_User_Qualification>(u => true);

            if (memoryList == null) return;

            int key = ((int)e.Keys[0]);
            string description = e.NewValues["Desciption"] == null ? string.Empty : e.NewValues["Desciption"].ToString();
            DateTime? time = e.NewValues["RowTime"] == null ? null : new DateTime?(Convert.ToDateTime(e.NewValues["RowTime"]));

            var entity = memoryList.Where(u => u.Item.Idx == key).FirstOrDefault();
            if (entity.Status == EditableItemStatus.Default)
                entity.Status = EditableItemStatus.Edit;
            entity.Item.Desciption = description;
            entity.Item.RowTime = time;

            Set(AspGridViewName, memoryList);
        }

        protected override void PreInserting(ResumeParam p, DevExpress.Web.Data.ASPxDataInsertingEventArgs e, ASPxGridView sender) {
            var memoryList = Get<TL_User_Qualification>(u => true);

            if (memoryList == null) return;

            string description = e.NewValues["Desciption"] == null ? string.Empty : e.NewValues["Desciption"].ToString();
            DateTime? time = e.NewValues["RowTime"] == null ? null : new DateTime?(Convert.ToDateTime(e.NewValues["RowTime"]));

            EditableItem<TL_User_Qualification> entity = new EditableItem<TL_User_Qualification>();

            entity.Status = EditableItemStatus.Insert;
            entity.Item = new TL_User_Qualification();
            entity.Item.Desciption = description;
            entity.Item.ResumeId = p.ResumeIdx;
            entity.Item.Year = time.HasValue ? new int?(time.Value.Year) : null;

            entity.Item.RowTime = time;
            if (memoryList.Count > 0)
                entity.Item.Idx = memoryList.Select(u => u.Item.Idx).Max() + 1;
            else
                entity.Item.Idx = 9999;

            memoryList.Add(entity);

            Set(AspGridViewName, memoryList);
        }
    }
}